package hk.m4s.lr.repair.test.ui.equipment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.TabLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import framework.common.Constant;
import framework.common.baseui.UeBaseActivity;
import framework.common.zanetwork.core.ResponseCallback;
import framework.utils.SharedPreferencesUtils;
import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.DeviceModelResult;
import hk.m4s.lr.repair.test.model.NewsModel;
import hk.m4s.lr.repair.test.model.NoticeDeviceModel;
import hk.m4s.lr.repair.test.model.NoticeDeviceModelResult;
import hk.m4s.lr.repair.test.model.NoticeModelResult;
import hk.m4s.lr.repair.test.service.device.DeviceListervice;
import hk.m4s.lr.repair.test.ui.adapter.DeviceRequestsAdapter;
import hk.m4s.lr.repair.test.ui.adapter.NewsAdapter;
import hk.m4s.lr.repair.test.ui.webview.WebViewActivity;
import hk.m4s.lr.repair.test.utils.ToastUtil;
import hk.m4s.lr.repair.test.views.listview.VListView;
import okhttp3.Response;

/**
 * Created by qi.wang on 2017/3/27.
 * 设备维护请求
 */

public class DeviceRequestsActivity extends UeBaseActivity implements VListView.IXListViewListener{
    private Context context;
    private LayoutInflater inflater;
    private TabLayout mTabLayout;
    private VListView vlist;
    private List<NoticeDeviceModel> newsList=new ArrayList<>();
    private List<String> mTitleList = new ArrayList<String>();//页卡标题集合
    private DeviceRequestsAdapter deviceRequestsAdapter;
    private int pageNum = 0;
    private int pageSize = 10;
    private String type="0";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseSetContentView(R.layout.activity_device_request_layout);
        context=this;
        hiddenFooter();
        showGoBackBtn();
        setTitleText("设备维护请求");
        findBaseView();
    }
    void findBaseView(){
        mTitleList.add("未报价");
        mTitleList.add("已报价");
        mTitleList.add("已完成");
        mTitleList.add("全部");
        mTabLayout = (TabLayout)findViewById(R.id.tabs);
        vlist=(VListView) findViewById(R.id.vlist);
        vlist.setXListViewListener(this);

        mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList.get(0)));
        mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList.get(1)));
        mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList.get(2)));
        mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList.get(3)), true);
        mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                String selectNum=String.valueOf(tab.getPosition());
                if(selectNum.equals("0")){
                    type="-1";
                }else if(selectNum.equals("1")){
                    type="1";
                }else if(selectNum.equals("2")){
                    type="3";
                }else if(selectNum.equals("3")){
                    type="0";
                }
                getData("1");
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        deviceRequestsAdapter=new DeviceRequestsAdapter(context,newsList);
        vlist.setAdapter(deviceRequestsAdapter);
        vlist.setOnItemClickListener(clickItem);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData("1");
    }

    private void onLoad() {
        vlist.stopRefresh();
        vlist.stopLoadMore();
        Date data = new Date(System.currentTimeMillis());
        vlist.setRefreshTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
                Locale.CHINA).format(data));
    }

    @Override
    public void onRefresh() {
        // TODO Auto-generated method stub
        // getData();
        vlist.setPullLoadEnable(true);
        pageNum=0;
        getData("1");
    }

    @Override
    public void onLoadMore() {
        // TODO Auto-generated method stub
        vlist.setPullRefreshEnable(true);
        pageNum++;
         getData("2");
    }
    AdapterView.OnItemClickListener clickItem = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                long arg3) {
            // TODO Auto-generated method stub
            NoticeDeviceModel model=newsList.get(arg2-1);
            Intent aboutIntent=new Intent(DeviceRequestsActivity.this, DeviceReplayActivity.class);
            aboutIntent.putExtra("model",model);
            startActivity(aboutIntent);
        }

    };
    public void getData(final String refreshType){
        Map<String, Object> parmas = new HashMap<>();
        parmas.put("device_type", Constant.device_type);
        parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
        try {
            JSONObject jsondata = new JSONObject();
            jsondata.put("pageNum", pageNum);
            jsondata.put("pageSize", pageSize);
            jsondata.put("type", type);
            parmas.put("jsonText",jsondata.toString());
        }catch (JSONException e){

        }
        DeviceListervice.deviceBusinessList(context,parmas , new ResponseCallback<NoticeDeviceModelResult>() {
            @Override
            public void onSuccess(NoticeDeviceModelResult deviceModelResult) {
                onLoad();
                switch (refreshType) {
                    case "1":
                        newsList.clear();
                        if(deviceModelResult.getList()!=null) {
                            newsList.addAll(deviceModelResult.getList());
                            if (deviceModelResult.getList().size() <= pageSize) {
                                vlist.removeFooterView();
                                vlist.setPullLoadEnable(false);
                            } else {
                                vlist.showFooterView();
                                vlist.setPullLoadEnable(true);
                            }
                        }
                        break;
                    case "2":
                        newsList.addAll(deviceModelResult.getList());
                        if (deviceModelResult.getList().size() <= pageSize) {
                            vlist.removeFooterView();
                            vlist.setPullLoadEnable(false);
                        }
                        break;
                }
                deviceRequestsAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Response response, int errorCode, String errorMsg, Exception e) {

            }
        });
    }
}
